package epam.com.threads.entities;

import epam.com.threads.util.PropertiesReader;
import org.apache.log4j.Logger;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * User: Yelena_M
 * Date: 05.09.13
 * Time: 18:37
 * To change this template use File | Settings | File Templates.
 */
public class Port {

    private final static Logger LOGGER = Logger.getLogger(Ship.class);

    private static Port instance;
    private BlockingQueue<Wharf> wharfs = new LinkedBlockingQueue<Wharf>();
    private int capacityPort;

    private Port() {

    }

    public static Port getInstance() {
        return instance;
    }

    public int getCapacityPort() {
        return capacityPort;
    }

    public void setCapacityPort(int capacityPort) {
        this.capacityPort = capacityPort;
    }

    public void leaveWharf(Wharf wharf) throws InterruptedException {
        wharfs.put(wharf);
    }

    public Wharf moor() throws InterruptedException {
        Thread.sleep(1000);
        return wharfs.take();
    }

    public void createWharf(Wharf wharf) {
        wharfs.add(wharf);
    }

    public static void open() {
        if (instance == null) {
            instance = new Port();
        }
    }

    public static void makeAction(int typeAction, String shipName) throws InterruptedException {
        PropertiesReader propertiesReader = new PropertiesReader();
        Integer unloadingTime = Integer.parseInt(propertiesReader.getProperties("data.unloadingTime"));
        Integer loadingTime = Integer.parseInt(propertiesReader.getProperties("data.loadingTime"));
        Integer unloadingAndLoadingTime = unloadingTime + loadingTime;

        switch (typeAction) {
            case 0: {
                Thread.sleep(unloadingTime); //разгрузка
                LOGGER.info("Ship " + shipName + " is unloading, time: " + unloadingTime);
                break;
            }
            case 1: {
                Thread.sleep(loadingTime); //загрузка
                LOGGER.info("Ship " + shipName + " is loading, time: " + loadingTime);
                break;
            }
            case 2: {
                Thread.sleep(unloadingAndLoadingTime);
                LOGGER.info("Ship " + shipName + " is unloading and loading, time: " + unloadingAndLoadingTime);
                break;
            }
        }
    }

}
